import tkinter as tk
import redis
import threading
import time
# pyinstaller --onefile -windowed redis_delete_like.py
r = redis.Redis(host='localhost', port=6379, db=0)


def on_button_click():
    print("按钮被点击了！")

    pattern = "2024*"
    cursor = '0'
    while cursor != 0:
        cursor, keys = r.scan(cursor=cursor, match=pattern)
        for key in keys:
            print(key.decode('utf-8'))

            r.delete(key.decode('utf-8'))

    print("2024清除完毕！")

def on_button_click_202501():
    print("按钮被点击了！")

    pattern = "202501*"
    cursor = '0'
    while cursor != 0:
        cursor, keys = r.scan(cursor=cursor, match=pattern)
        for key in keys:
            print(key.decode('utf-8'))

            r.delete(key.decode('utf-8'))

    print("202501清除完毕！")

def on_button_click_202502():
    print("按钮被点击了！")

    pattern = "202502*"
    cursor = '0'
    while cursor != 0:
        cursor, keys = r.scan(cursor=cursor, match=pattern)
        for key in keys:
            print(key.decode('utf-8'))

            r.delete(key.decode('utf-8'))

    print("202502清除完毕！")

def on_button_click_202503():
    print("按钮被点击了！")

    pattern = "202503*"
    cursor = '0'
    while cursor != 0:
        cursor, keys = r.scan(cursor=cursor, match=pattern)
        for key in keys:
            print(key.decode('utf-8'))

            r.delete(key.decode('utf-8'))

    print("202503清除完毕！")

def on_button_click_202504():
    print("按钮被点击了！")

    pattern = "202504*"
    cursor = '0'
    while cursor != 0:
        cursor, keys = r.scan(cursor=cursor, match=pattern)
        for key in keys:
            print(key.decode('utf-8'))

            r.delete(key.decode('utf-8'))

    print("202504清除完毕！")

def on_button_click_YYYYMM():
    print("按钮被点击了！")

    pattern = "202504*"
    cursor = '0'
    while cursor != 0:
        cursor, keys = r.scan(cursor=cursor, match=pattern)
        for key in keys:
            print(key.decode('utf-8'))

            r.delete(key.decode('utf-8'))

    print("202504清除完毕！")

# 创建主窗口
root = tk.Tk()
root.title("删除缓存")




# button = tk.Button(root, text="2024年", command=on_button_click)
# button.pack()
#
# button1 = tk.Button(root, text="2025年01月", command=on_button_click_202501)
# button1.pack()
#
# button2 = tk.Button(root, text="2025年02月", command=on_button_click_202502)
# button2.pack()
#
# button3 = tk.Button(root, text="2025年03月", command=on_button_click_202503)
# button3.pack()
#
# button4 = tk.Button(root, text="2025年04月", command=on_button_click_202504)
# button4.pack()

# 创建输入框
entry = tk.Entry(root, width = 20)
entry.pack()

entryButton = tk.Button(root, text="删除指定月份", command="")

is_running = False

def start_thread():
    global is_running
    print(is_running)
    if not is_running:
        is_running = True
        thread = threading.Thread(target=on_button_click)
        thread.daemon = True
        thread.start()

def start_thread_202501():
    thread01 = threading.Thread(target=on_button_click_202501)
    thread01.daemon = True
    thread01.start()

def start_thread_202502():
    print("202502")
    thread02 = threading.Thread(target=on_button_click_202502)
    thread02.daemon = True
    thread02.start()

def start_thread_202503():
    print("202503")
    thread03 = threading.Thread(target=on_button_click_202503)
    thread03.daemon = True
    thread03.start()

def start_thread_202504():
    print("202503")
    thread04 = threading.Thread(target=on_button_click_202504)
    thread04.daemon = True
    thread04.start()

def stop_thread():
    global is_running
    print(is_running)
    is_running = False

start_button = tk.Button(root, text="启动线程 删除2024年", command=start_thread)
start_button.pack()
start_button_202501 = tk.Button(root, text="启动线程_删除202501", command=start_thread_202501)
start_button_202501.pack()
start_button_202502 = tk.Button(root, text="启动线程_删除202502", command=start_thread_202502)
start_button_202502.pack()
start_button_202503 = tk.Button(root, text="启动线程_删除202503", command=start_thread_202503)
start_button_202503.pack()
start_button_202504 = tk.Button(root, text="启动线程_删除202504", command=start_thread_202504)
start_button_202504.pack()

stop_button = tk.Button(root, text="停止线程", command=stop_thread)
stop_button.pack()

root.mainloop()